﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CloudSoft.Extensions;

namespace ERPStore.Exporter.Providers
{
	public class LeguideExport : ExportBase
	{
		private StringBuilder m_Document;

		public LeguideExport()
			: base()
		{
		}

		public override void InitializeSettings(Models.Settings settings)
		{
			settings.AllowMissingDefaultImage = false;
			settings.AllowNotSellable = false;
			settings.AllowNullLongDescription = false;
			settings.AllowNullShortDescription = false;
			settings.AllowStockEmpty = false;
			settings.AllowZeroSalePrice = false;
			settings.FileName = "leguide.txt";
		}

		public override string LeadSourceId
		{
			get
			{
				return "EE1ECED1-A717-4c52-91C5-11E59DDED59C";
			}
		}

		public override string Name
		{
			get { return "leguide"; }
		}

		public override void CreateDocument()
		{
			m_Document = new System.Text.StringBuilder();
			m_Document.AppendLine("categorie|identifiant_unique|titre|description|prix|url_produit|url_image|frais_de_livraison|disponibilite|delais_de_livraison|garantie|reference_modele|D3E|marque|ean|prix_barre|devise|occasion|type_promotion|URL_mobile");
		}

		public override void AddElementToDocument(ERPStore.Models.Product product, ERPStore.Models.ProductStockInfo productStock)
		{
			var categorie = GetDeepCategoryName(product);
			var identifiant_unique = product.Code;
			var titre = (product.PageTitle ?? product.Title).Replace("|", " ");
			var prix = string.Format("{0:F2}", Math.Max(product.MinimumSaleQuantity * product.BestPrice.ValueWithTax, product.Packaging.Value * product.BestPrice.ValueWithTax));
			var description = string.Empty;
			description += product.ShortDescription.DefaultValueIfNullOrTrimmedEmpty(product.Title).Replace(System.Environment.NewLine, " ");
			if (product.MinimumSaleQuantity > 0)
			{
				description += string.Format(" tarif pour une quantité minimale de {0} ", product.MinimumSaleQuantity);
			}
			if (product.Packaging.Value > 0)
			{
				description += string.Format(" tarif vendu par {0} ", product.Packaging.Value);
			}
			var URL_produit = BuildEncryptedProductUrl(LeadSource, product);
			var URL_image = string.Format("http://{0}{1}", GlobalConfiguration.Configuration.Settings.DefaultUrl, string.Format(product.DefaultImage.Url, 0, 0));
			var defaultCountry = GlobalConfiguration.Configuration.Settings.Shipping.DeliveryCountryList[0];
			var frais_de_port = ToCurrency(defaultCountry.ShippingFeeAmount,true);
			if (product.Shipping.Strategy == ERPStore.Models.FeeTransportStrategy.FixedPrice)
			{
				frais_de_port = ToCurrency(product.Shipping.FixedPrice,true);
			}
			else if (product.BestPrice.Value > defaultCountry.MinimalFreeOfCarriageAmount)
			{
				frais_de_port = "0";
			}

			string disponibilite = "0";
			int delai_de_livraison = 0;
			if (productStock != null)
			{
				disponibilite = (productStock.HasStock) ? "0" : productStock.TotalProvisionningDayCount.ToString();
				delai_de_livraison = (productStock.HasStock) ? 2 : Math.Max(2, productStock.DeliveryDaysCount);
			}
			else
			{
				disponibilite = "5";
				delai_de_livraison = 5;
			}
			var garantie = (product.Warranty.IsNullOrTrimmedEmpty()) ? "0" : "1";
			var reference_modele = "";
			var D3E = (product.RecyclePrice != null) ? ToCurrency(product.RecyclePrice.ValueWithTax,true) : "";
			var marque = (product.Brand != null) ? product.Brand.Name : "";
			var EAN = "";
			var prix_barre = (product.StrikedPrice != null) ? ToCurrency(product.StrikedPrice.ValueWithTax) : "";
			var devise = "EUR";
			var occasion = "0";
			var type_promotion = "0"; //  (product.SelectedOffer == ERPStore.Models.PriceType.Promotional) ? "1" : "0";
			var URL_mobile = "";

			m_Document.AppendFormat("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}|{13}|{14}|{15}|{16}|{17}|{18}|{19}\r\n"
				, categorie
				, identifiant_unique
				, titre
				, description
				, prix
				, URL_produit
				, URL_image
				, frais_de_port
				, disponibilite
				, delai_de_livraison
				, garantie
				, reference_modele
				, D3E
				, marque
				, EAN
				, prix_barre
				, devise
				, occasion
				, type_promotion
				, URL_mobile
				);
		}

		public override void SaveDocument()
		{
			var path = Configuration.ConfigurationSettings.AppSettings["exportPath"];
			var fileName = System.IO.Path.Combine(path, Settings.FileName);
			if (System.IO.File.Exists(fileName))
			{
				System.IO.File.Delete(fileName);
			}

			var content = m_Document.ToString();
			System.IO.File.WriteAllText(fileName, content, System.Text.Encoding.UTF8);
		}

		public override void Dispose()
		{
			m_Document = null;
		}
	}
}
